<?php

namespace app\admin\controller\auth;

use app\admin\controller\AdminController;
use app\admin\validate\auth\RoleValidate;
use app\admin\service\auth\RoleService;
use think\response\Json;

/**
 * 角色控制器
 */
class Role extends AdminController
{

    /**
     * 查看角色列表
     * @return Json
     */
    public function lists()
    {
        return $this->data(RoleService::getList());
    }


    /**
     * 添加角色
     * @return Json
     */
    public function add()
    {
        $params = (new RoleValidate())->post()->checkParam('add');
        $res = RoleService::add($params);
        if (true === $res) {
            return $this->success([],'添加成功');
        } else {
            return $this->error(RoleService::getError());
        }
    }


    /**
     * 编辑角色
     * @return Json
     */
    public function edit()
    {
        $params = (new RoleValidate())->post()->checkParam('edit');
        $res = RoleService::edit($params);
        if (true === $res) {
            return $this->success([],'编辑成功');
        } else {
            return $this->error(RoleService::getError());
        }
    }


    /**
     * 删除角色
     * @return Json
     */
    public function delete()
    {
        $params = (new RoleValidate())->delete()->checkParam('del');
        $res = RoleService::delete($params['id']);
        if (true === $res) {
            return $this->success([],'删除成功');
        } else {
            return $this->error(RoleService::getError());
        }
    }


    /**
     * 查看角色详情
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail()
    {
        $params = (new RoleValidate())->checkParam('detail');
        $detail = RoleService::detail($params['id']);
        return $this->data($detail);
    }


    /**
     * 获取角色数据
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function all()
    {
        $result = RoleService::getAllData();
        return $this->data($result);
    }

}